package com.codeoss.module.stock.pagebean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.view.component.Message;
import com.codeoss.framework.view.pagebean.FormViewPageBean;
import com.codeoss.module.account.model.AccountInvoice;
import com.codeoss.module.account.service.IAccountInvoiceService;
import com.codeoss.module.sale.model.SaleReturn;
import com.codeoss.module.sale.service.ISaleReturnService;
import com.codeoss.module.stock.model.StockOutConfirm;
import com.codeoss.module.stock.service.IStockOutConfirmService;

@Service
public class StockOutFormViewPageBean extends FormViewPageBean implements ApplicationContextAware {
	@Autowired
    private IStockOutConfirmService stockOutConfirmService;
	private ApplicationContext applicationContext;
	@Autowired 
	private IAccountInvoiceService accountInvoiceService;
	@Transactional
	public void generateStockOutConfirm() {
		// TODO Auto-generated method stub
		YunContext yunContext=YunContext.getCurrentContext();
		Long keyValue=(Long)getCurrentKeyValue();
		StockOutConfirm stockOutConfirm=stockOutConfirmService.generateConfirm(keyValue);
		yunContext.addMessage(new Message("生成出库确认单 单据号"+stockOutConfirm.getDocumentNo()));
	}
	
	@Transactional
	public void generateSaleReturn() {
		// TODO Auto-generated method stub
		YunContext yunContext=YunContext.getCurrentContext();
		Long keyValue=(Long)getCurrentKeyValue();
		
		ISaleReturnService saleReturnService=applicationContext.getBean(ISaleReturnService.class);
		SaleReturn saleReturn=saleReturnService.generateSaleReturn(keyValue);
		yunContext.addMessage(new Message("生成销售退货单 单据号"+saleReturn.getDocumentNo()));
	}

	@Transactional
	public void generateInvoice() {
		// TODO Auto-generated method stub
		YunContext yunContext=YunContext.getCurrentContext();
		Long keyValue=(Long)getCurrentKeyValue();
		AccountInvoice accountInvoice=accountInvoiceService.generateInvoiceFromStockOut(keyValue);
		yunContext.addMessage(new Message("生成发票 单据号"+accountInvoice.getDocumentNo()));
	}
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext=applicationContext;
	}
}
